;layui.define(['jquery', 'fa_side', 'fa_tab'], function (exports) {
    let $ = layui.$,
        fa_side = layui.fa_side,
        fa_tab = layui.fa_tab,
        MOD_NAME = "fa_nav",
        debug = true,
        log = function log(...msg) {
            debug && console.log(MOD_NAME, ...msg)
        };
    //fa_nav 模块
    let Fa_Nav = function (options) {
        this.options = {
            nav_cls: "fa-nav",
            dir_cls: "fa-nav-dir",
            item_cls: "fa-nav-item",
            item_d_cls: "fa-nav-itemed",
            shrink: false,
        };
        $.extend(this.options, options);
        this.init();
    }
    Fa_Nav.prototype.init = function () {
        let _this = this,
            o = _this.options,
            box = $("." + o.nav_cls);
        box.children("." + o.dir_cls).on('click', function (event) {
            let self = $(this),
                isOn = self.hasClass(_this.options.item_d_cls);
            fa_side.open();
            if (isOn) {
                _this.options.shrink ? _this.clearDir() : _this.closeDir(self);
            } else {
                _this.openDir(self)
            }
            event.stopPropagation();
            return false;
        })
        box.children("." + o.item_cls).on('click', function (event) {
            fa_side.open();
            _this.options.shrink && _this.clearDir();
            _this.openItem($(this));
            event.stopPropagation();
            return false;
        })
        log("init")
    }
    Fa_Nav.prototype.clearItem = function () {
        $("." + this.options.item_cls).removeClass(this.options.item_d_cls);
    }
    Fa_Nav.prototype.clearDir = function () {
        $("." + this.options.dir_cls).removeClass(this.options.item_d_cls);
    }
    Fa_Nav.prototype.myDir = function (ele) {
        //1级菜单没有dir
        if (!ele.parent().hasClass('fa-nav-box')) {
            return ele.parent().prev();
        }
        return false;
    }
    Fa_Nav.prototype.hasDir = function (ele) {
        return ele.next().children("." + this.options.dir_cls)
    }
    Fa_Nav.prototype.itemed = function (ele) {
        ele.addClass(this.options.item_d_cls);
        let mydir = this.myDir(ele);
        mydir && this.openDir(mydir);
    }
    Fa_Nav.prototype.openDir = function (ele) {
        let mydir = this.myDir(ele);
        ele.addClass(this.options.item_d_cls);
        mydir && this.openDir(mydir);
    }
    Fa_Nav.prototype.closeDir = function (ele) {
        let hasdir = this.hasDir(ele)
        log('hasdir', hasdir)
        ele.removeClass(this.options.item_d_cls);
        hasdir && hasdir.removeClass(this.options.item_d_cls);
    }
    Fa_Nav.prototype.openItem = function (ele) {
        this.clearItem();
        this.itemed(ele);
        let title = ele.attr('title'),
            url = ele.attr('fa-href'),
            target = ele.attr('fa-target');
        fa_tab.open(title, url, target);
    }
    Fa_Nav.prototype.close = function (ele, parent) {
    }
    exports(MOD_NAME, {
        nav: new Fa_Nav(),
        open: function (url, target) {
        },
        close: function () {
        }, clear: function () {
            this.nav.clear();
        }
    })
})